package td3.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;

import td3.model.AppModel;
import td3.model.Contact;
import td3.model.ContactNodeTreeTransferHandler;
import td3.model.ContactTreeModel;
import td3.model.AppModel.Property;

public class ContactTreePane extends JPanel implements TreeSelectionListener {

	private static final long serialVersionUID = -122276698071287045L;

	private JTree tree;
	private final AppModel appModel;

	public ContactTreePane(final AppModel appModel) {
		super(new BorderLayout());
		this.appModel = appModel;
		this.setBorder(BorderFactory.createTitledBorder("Contacts"));
		this.setBackground(Color.WHITE);
		this.tree = new JTree(appModel.getContactTreeModel());
		this.tree.setRootVisible(false);
		this.tree.getSelectionModel().setSelectionMode(
				TreeSelectionModel.SINGLE_TREE_SELECTION);
		this.tree.addTreeSelectionListener(this);
		this.add(tree, BorderLayout.CENTER);
		this.tree.setTransferHandler(new ContactNodeTreeTransferHandler());
		this.tree.setDragEnabled(true);
		appModel.addPropertyChangeListener(new PropertyChangeListener() {

			@Override
			public void propertyChange(PropertyChangeEvent event) {
				if (event.getPropertyName().equals(
						Property.CONTACT_TREE_MODEL.name())) {
					ContactTreeModel contactTreeModel = (ContactTreeModel) event
							.getNewValue();

					contactTreeModel
							.addTreeModelListener(new TreeModelListener() {

								@Override
								public void treeStructureChanged(
										TreeModelEvent arg0) {
									expandAllTree();
								}

								@Override
								public void treeNodesRemoved(TreeModelEvent arg0) {
								}

								@Override
								public void treeNodesInserted(
										TreeModelEvent arg0) {
								}

								@Override
								public void treeNodesChanged(TreeModelEvent arg0) {
								}
							});
					tree.setModel(contactTreeModel);
					expandAllTree();
				}
			}
		});

	}

	public void expandAllTree() {
		int iRow = 0;
		while (iRow < tree.getRowCount()) {
			tree.expandRow(iRow);
			iRow++;
		}
	}

	@Override
	public void valueChanged(TreeSelectionEvent arg0) {
		DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree
				.getLastSelectedPathComponent();
		if (node != null) {
			Object nodeInfo = node.getUserObject();
			if (nodeInfo instanceof Contact) {
				Contact contact = (Contact) nodeInfo;
				appModel.setCurrentContact(contact);
			} else {
				appModel.setCurrentContact(null);
			}
		}
	}
}
